package com.kgc.fk.Controller;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.fk.Service.BuyPayServ;
import com.kgc.fk.Service.PublicConServ;
import com.kgc.skconsumer.feign.CwWorkFlowFeign;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("CGFK")
public class BuyPayController {

    private Logger logger = LoggerFactory.getLogger(BuyPayController.class);

    @Resource
    private BuyPayServ buyPayServ;

    @Resource
    private PublicConServ publicConServ;

    @Resource
    private CwWorkFlowFeign cwWorkFlowFeign;


    @RequestMapping("getPage")
    public ResponseMsg getPage(@RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = buyPayServ.getBuyPayPage(map);
        return responseMsg;
    }

    @RequestMapping("delBuyPay")
    public ResponseMsg delBuyPay(String code) {
        Map<String,Object> map = new HashMap<>();
        map.put("del" , 1);
        map.put("code", code);
        ResponseMsg responseMsg = buyPayServ.updateBuyPay(map);
        return responseMsg;
    }

    @RequestMapping("addBuyPay")
    public ResponseMsg addBuyPay(@RequestBody Map<String,Object> map) {
        String str = publicConServ.getCode("cw_fk_pay");
        map.put("code", "CGFK-" + str);
        ResponseMsg responseMsg = buyPayServ.addBuyPay(map);
        Map<String,Object> variables = new HashMap<>();
        variables.put("code","CGFK-" + str);//单据编号
        variables.put("type",map.get("type"));//流程类型
        //创建一个流程实例
        com.kgc.activiti.util.ResponseMsg res = cwWorkFlowFeign.startProcess(variables);
        if (!"200".equals(res.getCode())){
            responseMsg.setCode(res.getCode());
        }
        return responseMsg;
    }

    @RequestMapping("getBuyPay")
    public ResponseMsg getBuyPay(@RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = buyPayServ.getBuyPay(map);
        return responseMsg;
    }

    @RequestMapping("modiBuyPay")
    public ResponseMsg modiBuyPay(@RequestBody Map<String,Object> map) {
        ResponseMsg responseMsg = buyPayServ.modiBuyPay(map);
        return responseMsg;
    }

    @RequestMapping("/check")
    public  ResponseMsg check(@RequestBody Map<String,Object> map){
        ResponseMsg responseMsg = new ResponseMsg();
        logger.info("CGXJController check params,{}",map.toString());
        Map<String,Object> variables = new HashMap<>();
        variables.put("code",map.get("code"));//单据编号
        variables.put("checkManId",Integer.valueOf((String) map.get("checkManId")));//审批人
        variables.put("checkManName",(String) map.get("checkManName"));//审批人
        variables.put("checkComment",(String) map.get("checkComment"));//审批意见
        variables.put("checkDate",new Date());//审批时间
        variables.put("checkRes",(boolean)map.get("checkRes"));//审批结果
        com.kgc.activiti.util.ResponseMsg msg=  cwWorkFlowFeign.competeTask(variables);
        responseMsg.setCode(msg.getCode());
        responseMsg.setMsg(msg.getMsg());
        return responseMsg;
    }
}
